package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //添加商品
    @Override
    public void addGoods(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//数据拷贝过去
        //当前用户的id
        Long userId = BaseContext.getCurrentId();
        System.out.println(userId);
        shoppingCart.setUserId(userId);
        //先查询数据是否有该菜品的数据
        List<ShoppingCart> goods = shoppingCartMapper.getGoods(shoppingCart);
        //如果有查到 那么就在那数据的数量上加 1 就行
        if (goods != null && goods.size() > 0) {
            ShoppingCart good = goods.get(0);//即使添加力两个一样的数据查出来的集合还是只有一条数据
            System.out.println(good.getNumber());
            good.setNumber(good.getNumber() + 1);//在原先的数量上加1
            shoppingCartMapper.updateNumberById(good);//执行更新操作
        } else {
            //没有查到就插入
            //判断传过来的是套餐还是菜品
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId != null) {
                //查出菜品 设置属性
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);//新增数据
        }
    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list();
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //判断该商品的数量是否大于1
        Integer count = shoppingCartMapper.getCount(shoppingCartDTO);
        if (count <=1) {
            //删除操作
            shoppingCartMapper.removeGoods(shoppingCartDTO);
        }
        //数量减
        shoppingCartMapper.subCount(shoppingCartDTO);
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteShopCart();
    }
}
